package ee.difference.util;

import java.io.IOException;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.NoSuchElementException;

public class CamelCaseTokenizer implements Enumeration<String> {
	
	String target;
	StringReader reader;
	int[] current = new int[2];
	boolean atEndOfStream = true;
	
	public CamelCaseTokenizer(String str){
		if(str == null) 
			target = "";
		else 
			target = str;
		
		reader = new StringReader(target);
		try {
			current[0] = reader.read();
			current[1] = reader.read();
		} catch (IOException e) {
			current[0] = -1;
			current[1] = -1;
			atEndOfStream = true;
		}
		
	}
	
	public boolean hasMoreElements() {
		return (!atEndOfStream);
	}

	public String nextElement(){
		
		boolean atEndOfToken = false;
		StringBuffer buffer = new StringBuffer("");
		int counter = 0;

		try {
			while((!atEndOfToken) && (counter < target.length())){
				
				buffer.append((char)this.current[0]);
				current[0] = current[1];
				current[1] = reader.read();
				counter++;
				
				if(Character.isUpperCase((char)this.current[0]) 
						&& (Character.isLowerCase((char)this.current[1]))){
					// ここまでの文字列をトークンとして返す。
					atEndOfToken = true;
				}
				else if(current[0] == -1 && current[1] == -1){
					// ここまでの文字列をトークンとして返す。
					atEndOfToken = true;
					//　ストリームの終了
					atEndOfStream = true;
				}
				
			}
		} catch (IOException e) {
			throw new NoSuchElementException(e.getLocalizedMessage());
		}
		
		
		return buffer.toString();
	}

}
